#include "api_cstructs.h"


void __free_json_list(jPtr);
void __free_json_object(jPtr);


void _free_evtptr(eventPtr ptr){
	int i;
	if(ptr == 0)
		return;
	for(i = 0; ptr[i].event_id != 0 || ptr[i].event_name != 0; i++){
		if(ptr[i].event_id != 0)
			free(ptr[i].event_id);
		if(ptr[i].event_name != 0)
			free(ptr[i].event_name);
	}
	free(ptr);
}


void _free_mapptr(mapPtr ptr){
	int i;
	if(ptr == 0)
		return;
	for(i = 0; ptr[i].events != 0 || ptr[i].map_name != 0; i++){
		if(ptr[i].map_name != 0)
			free(ptr[i].map_name);
		if(ptr[i].events != 0)
			_free_evtptr(ptr[i].events);
	}
	free(ptr);
}


void _free_worldptr(worldPtr ptr){
	int i;
	if(ptr == 0)
		return;
	for(i = 0; ptr[i].world_name != 0 || ptr[i].maps != 0; i++){
		if(ptr[i].world_name != 0)
			free(ptr[i].world_name);
		_free_mapptr(ptr[i].maps);
	}
	free(ptr);
}


void _free_jptr(jPtr ptr){
	if(ptr == 0)
		return;
	switch(ptr->data_type){
	case List:
		__free_json_list(ptr);
		break;
	case Object:
		__free_json_object(ptr);
		break;
	default:
		free(ptr);
		break;
	}
}


void __free_json_list(jPtr ptr){
	int i, numeric;
	numeric = 1;
	for(i = 0; ptr->data[i].data_type != end && numeric == 1; i++){
		switch(ptr->list_type){
		case Object:
			__free_json_object(ptr->data[i].data);
			break;
		case List:
			__free_json_list(ptr->data[i].data);
			break;
		case String:
			free(ptr->data[i].string);
			break;
		default:
			numeric = 0;
			break;
		}
	}
	free(ptr->data);
	free(ptr);
}


void __free_json_object(jPtr ptr){
	int i, numeric;

	numeric = 1;
	for(i = 0; ptr[i].data_type != end; i++){
		free(ptr[i].key);
		switch(ptr[i].data_type){
		case List:
			__free_json_list(ptr[i].data);
			break;
		case Object:
			__free_json_object(ptr[i].data);
			break;
		case String:
			free(ptr[i].string);
			break;
		default:
			break;
		}
	}
	free(ptr);
}


void _free_guild_ptr(guildPtr ptr){
	free(ptr->id);
	free(ptr->name);
	free(ptr->tag);
	free(ptr);
}